<?php
/**
 * | ---------------------------------------------------------------------------------------------------
 * | ProjectName: mvc
 * | ---------------------------------------------------------------------------------------------------
 * | Author：johnxu <fsyzxz@163.com>
 * | ---------------------------------------------------------------------------------------------------
 * | Home: https://www.xfjpeter.cn
 * | ---------------------------------------------------------------------------------------------------
 * | Data: 201907202019-07-20
 * | ---------------------------------------------------------------------------------------------------
 * | Desc:
 * | ---------------------------------------------------------------------------------------------------
 */

namespace app\common\libs;

class Tree
{
    /**
     * 获取树状结构下拉菜单
     * @param     $data
     * @param int $pid
     * @return array
     */
    public static function getRuleTree($data, $pid = 0)
    {
        $result = [];
        foreach ($data as $key => $val) {
            if ($val['pid'] == $pid) {
                $result[] = [
                    'name'        => $val['title'],
                    'icon'        => $val['icon'],
                    'label'       => $val['title'],
                    'description' => $val['description'],
                    'value'       => $val['id'],
                    'children'    => self::getRuleTree($data, $val['id']),
                ];
            }
        }

        foreach ($result as $key => $item) {
            if (isset($item['children']) && empty($item['children'])) {
                unset($result[$key]['children']);
            }
        }

        return $result;
    }

    /**
     * 获取规则树状列表
     * @param     $data
     * @param int $pid
     * @return array
     */
    public static function getRule($data, $pid = 0)
    {
        $result = [];
        foreach ($data as $key => $val) {
            if ($val['pid'] == $pid) {
                $result[] = [
                    'pid'         => $val['pid'],
                    'id'          => $val['id'],
                    'title'       => $val['title'],
                    'name'        => $val['title'],
                    'icon'        => $val['icon'],
                    'label'       => $val['title'],
                    'description' => $val['description'],
                    'method'      => $val['method'],
                    'module'      => ucfirst($val['module']),
                    'controller'  => ucfirst($val['controller']),
                    'action'      => ucfirst($val['action']),
                    'router'      => $val['router'],
                    'is_menu'     => $val['is_menu'],
                    'is_show'     => $val['is_show'],
                    'status'      => $val['status'],
                    'children'    => self::getRule($data, $val['id']),
                ];
            }
        }

        foreach ($result as $key => $item) {
            if (isset($item['children']) && empty($item['children'])) {
                unset($result[$key]['children']);
            }
        }

        return $result;
    }
}